﻿
var onlyOpenTitle = "系统首页";
var opentabs = 8; //允许打开的TAB数量

function LeftSonSetCss(e) {
   var tmpNode = e.parentNode.childNodes;
   for (var i=0 ; i<tmpNode.length; i++) {
	   tmpNode[i].className="scdDvN";
   }
   e.className = "scdDvY";
}

//---==tabWindows in main
function addTab(subtitle, url, icon) {
    if (url == "" || url == "#")
        return false;
    var tabCount = $('#tabs').tabs('tabs').length;
    var hasTab = $('#tabs').tabs('exists', subtitle);
    var add = function () {
        if (!hasTab) {
            $('#tabs').tabs('add', {
                title: subtitle,
                content: createFrame(url),
                closable: true,
                icon: icon
            });
        } else {
            $('#tabs').tabs('select', subtitle);
            //closeTab('refresh'); //选择TAB时刷新页面
        }
    };
    if (tabCount > opentabs && !hasTab) {
        var msg = '<span class="f14 spLRed">您打开了太多的功能菜单，如果继续打开，会造成程序运行缓慢，无法流畅操作！</span>';
        $.messager.confirm("系统提示", msg, function (b) {if (b) add();else return false;});
    } else {
        add();
    }
}

function createFrame(url) {
    var s = '<iframe scrolling="auto" frameborder="0"  style="width:100%;height:99.5%;" src="' + url + '" ></iframe>';
    return s;
}

function tabClose() {
    /*双击关闭TAB选项卡*/
    $(".tabs-inner").live('dblclick', function () {
        var subtitle = $(this).children(".tabs-closable").text();
        if (subtitle != onlyOpenTitle && subtitle != "")
            $('#tabs').tabs('close', subtitle);
    });
}

function closeTab(action) {
    var alltabs = $('#tabs').tabs('tabs');
    var currentTab = $('#tabs').tabs('getSelected');
    var allTabtitle = [];
    $.each(alltabs, function (i, n) {
        allTabtitle.push($(n).panel('options').title);
    });
    switch (action) {
        case "refresh":
            var iframe = $(currentTab.panel('options').content);
            var src = iframe.attr('src');
            $('#tabs').tabs('update', {
                tab: currentTab,
                options: {
                    content: createFrame(src)
                }
            });
            break;
        case "close":
            var currtab_title = currentTab.panel('options').title;
            $('#tabs').tabs('close', currtab_title);
            break;
        case "closeall":
            $.each(allTabtitle, function (i, n) {
                if (n != onlyOpenTitle) {
                    $('#tabs').tabs('close', n);
                }
            });
            break;
        case "closeother":
            var currtab_title = currentTab.panel('options').title;
            $.each(allTabtitle, function (i, n) {
                if (n != currtab_title && n != onlyOpenTitle) {
                    $('#tabs').tabs('close', n);
                }
            });
            break;
        case "closeright":
            var tabIndex = $('#tabs').tabs('getTabIndex', currentTab);

            if (tabIndex == alltabs.length - 1) {
                //alert('系统提示：后边没有标签!');
				$.messager.show({title:'操作提示',msg:'<br><span class="f14 spLRed">后边没有功能页面标签！</span>',showType:'slide',timeout:2000,style:{right:'',top:document.body.scrollTop+document.documentElement.scrollTop,bottom:''}});
                return false;
            }
            $.each(allTabtitle, function (i, n) {
                if (i > tabIndex) {
                    if (n != onlyOpenTitle) {
                        $('#tabs').tabs('close', n);
                    }
                }
            });

            break;
        case "closeleft":
            var tabIndex = $('#tabs').tabs('getTabIndex', currentTab);
            if (tabIndex == 1) {
                //alert('系统提示：前边没有标签，不能关闭!');
				$.messager.show({title:'操作提示',msg:'<br><span class="f14 spLRed">前边没有功能页面标签，不能关闭！</span>',showType:'slide',timeout:2000,style:{right:'',top:document.body.scrollTop+document.documentElement.scrollTop,bottom:''}});
                return false;
            }
            $.each(allTabtitle, function (i, n) {
                if (i < tabIndex) {
                    if (n != onlyOpenTitle) {
                        $('#tabs').tabs('close', n);
                    }
                }
            });

            break;
        case "exit":
            $('#closeMenu').menu('hide');
            break;
    }
}

function initMainTabWin() {
    $('#tabs').tabs({
        tools: [{
            iconCls: 'icn-x_refresh',
            handler: function() {
                var tab = $('#tabs').tabs('getSelected');
                if (tab.panel('options').title != onlyOpenTitle)
                    closeTab('refresh');
                return false;
            }
        }, {
            iconCls: 'icn-x_cancel',
            handler: function() {
				/*
                if (confirm('系统提示：确认要关闭所有窗口吗？')) {
                    closeTab("closeall");
                }
				*/
				$.messager.defaults.ok='确 定';
				$.messager.defaults.cancel='取 消';
				$.messager.confirm("操作提示", '<br><span class="f14 spLBlue">确认要关闭所有窗口吗？</span>', function (b) {if (b) closeTab("closeall");else return false;})
            }
        }],
        onContextMenu: function(e, title) {
            e.preventDefault();
            $('#closeMenu').menu('show', {
                left: e.pageX,
                top: e.pageY
            });
            $('#tabs').tabs('select', title);
        }
    });
}
//---tabWindows in main==

//---==Left&Bottom Blocks

function showCopyright() {
    document.write("Copyright&copy;市政务服务管理和网络理政办公室 2013-2014 &nbsp;&nbsp;  <!--运行维护：成都市软件产业发展中心 &nbsp;&nbsp;联系电话：028-xxxxxxxx &nbsp;&nbsp; 传真：028-yyyyyyyy -->");
}
//---Left&Bottom Blocks==


$(function () {
    //tabClose();
    initMainTabWin();
	//alert(leftBkHtml);
    $('body').layout({onExpand: function () { $('body').layout('resize'); } });
    //Tab右键菜单
    $('#closeMenu').menu({onClick: function (item) {closeTab(item.id);} });
});